# /usr/bin/python
# coding: utf-8
import pymysql
import csv


def readDb(dbConfig, DB_TABLE):
    # 打开数据库连接
    db = pymysql.connect(**dbConfig)
    cursor = db.cursor()
    sql = "select COLUMN_NAME,COLUMN_TYPE from information_schema.COLUMNS where table_name = '%s';" % (
        DB_TABLE)
    try:
        # 写入当前数据库字段
        with open('./csv.csv', 'w', encoding='utf8', newline='') as csvfile:  # 打开方式还可以使用file对象
            fieldnames = ['currentName', 'currentType', 'newName', 'newType']
            writer = csv.writer(csvfile)
            writer.writerow(fieldnames)
            cursor.execute(sql)
            result = cursor.fetchall()
            for row in result:
                writer.writerow(row)
    except Exception as e:
        print("发生错误", repr(e))
    finally:
        csvfile.close()
        db.close()
